using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;


namespace WebComponents.Templates
{
	namespace Design 
	{

		public class DisplayStatsDesigner : ControlDesigner
		{
			// private member variables
			private DisplayStats statsInstance;		// an instance of the class

			public override void Initialize(System.ComponentModel.IComponent component)
			{
				statsInstance = (DisplayStats) component;
				base.Initialize(component);
			}

			#region GetDesignTimeHtml Method
			public override string GetDesignTimeHtml()
			{
				StringWriter sw = new StringWriter();
				HtmlTextWriter writer = new HtmlTextWriter(sw);

				// Create a new StatsData instance based on the values entered into the
				// control's properties...
				statsInstance.AddStatsData(new StatsData(statsInstance.Title, statsInstance.TotalPostCount, statsInstance.TotalUserCount));

				// get the template
				ITemplate template = statsInstance.StatsTemplate;
				if (template == null)
					// the user has not specified a template, we need to generate a default template
					template = new DefaultStatsTemplate();

				// instantiate the StatsData in the template
				template.InstantiateIn(statsInstance.StatsData);
				statsInstance.DataBind();

				// get the HTML produced by the control
				statsInstance.RenderControl(writer);
				return sw.ToString();
			}
			#endregion		
		}
	}
}